;(function() {
	'use strict';
	angular.module('controllers.faultLocation.appAlarmAnalysis')
		.controller('analysisCtrl', analysisCtrl);

	function analysisCtrl($scope, System, appAlarmServer, analysisServer,CoreService,appStrainService,$timeout,CoreLoading) {
		$scope.shoumun = {
			num: true
		};
		$scope.sysNames = System;
		$scope.timeTypes = appAlarmServer.getTimeType();
		$scope.changeTime = appAlarmServer.changeTime;
		$scope.sendData = analysisServer.getPageBind();
		$scope.levels = analysisServer.queryAlarmLevel({});
		$scope.standardType = analysisServer.getStandardType();
		$scope.send = {
			page:{
				currentPage:1,
				pageSize:'10'
			}
		};
		$scope.search = function() {
			if ($scope.shoumun.num === true || $scope.myForm.$invalid) {
				$scope.shoumun.num = true;
				return;
			}
			//时间粒度为天时，最多只能查询31天的数据
			var endTime = $scope.sendData.endTime.getTime();
			var startTime = $scope.sendData.startTime.getTime();
			var day = (endTime - startTime) / 1000 / 60 / 60 / 24;
			var hour = parseInt((endTime - startTime) / 1000 / 60 / 60);
			if ($scope.sendData.timeType === '1') {
				if (day > 30) {
					$scope.shoumun.num = true;
					CoreService.dialog('粒度为天时,只能查询31天内的数据！');
					return;
				}
			} else {
				if (hour > 47) {
					$scope.shoumun.num = true;
					CoreService.dialog('粒度为小时时,只能查询48小时内的数据！');
					return;
				}
			}
			CoreLoading.setState(true);
			$scope.flag = 'view';
			$scope.send = analysisServer.fmtSendData($scope.sendData);
			$timeout(function(){
				analysisServer.queryComparedNum($scope.send,$scope.flag);
				//console.log($scope.flag);
			});

		};
		//切换表格
		$scope.changeTable = function() {
			CoreLoading.setState(true);
			appStrainService.queryDetStrainAppAlarmNum($scope.send, function(res) {
				CoreLoading.setState(false);
				if (res.state === '10000') {
					var temp = {
						echartsXData: [],
						title:'',
						level:'',
						systemName:''
					};
					angular.forEach(res.data.data, function(item) {
						temp.level = item.alarmLevelName;
						temp.systemName = item.systemName;
						if (temp.echartsXData.indexOf(item.time)) {
							temp.echartsXData.push(item.time);
						}
					});
					if(temp.echartsXData[0] === temp.echartsXData[temp.echartsXData.length - 1]){
						$scope.tableTitle = temp.echartsXData[0].replace(/-/g, '') + '应用告警数统计';
					}else {
						$scope.tableTitle = temp.echartsXData[0].replace(/-/g, '') + '到' + temp.echartsXData[temp.echartsXData.length - 1].replace(/-/g, '') + '应用告警数统计';
					}
					$scope.items = appAlarmServer.ypaging($scope.send, res.data);
					$scope.flag = 'table';
				} else {
					CoreService.dialog('查询错误');
				}
			});
		};
		//切换图形
		$scope.changeView = function() {
			$scope.flag = 'view';
		};
		//切换图形
		$scope.changeView = function() {
			$scope.flag = 'view';
		};
		$scope.sendDet = {
			page: {
				currentPage: 1,
				pageSize: '10'
			}

		};
		//表格下钻
		$scope.tableDown = function(item) {
			CoreLoading.setState(true);
			$scope.sendDet.alarmLevelCode = $scope.send.alarmLevelCode;
			$scope.sendDet.alarmStateCode = $scope.send.alarmStateCode;
			$scope.sendDet.systemCode = $scope.send.systemCode;
			$scope.sendDet.timeType = $scope.send.timeType;
			$scope.sendDet.time = item.time;
			$scope.sendDet.alarmNum = item.alarmNum;
			//总行数
			$scope.totalCount = item.alarmNum;
			$scope.downItmes = appAlarmServer.queryDetAlarmNums($scope.sendDet);
			$scope.titleDetTable = item.time.replace(/-/g, '')+'应用告警数详细统计';
			$scope.flag = 'tableDown';
		};
		$scope.back = function() {
			$scope.flag = 'table';
		};
	}
	analysisCtrl.$inject = ['$scope', 'System', 'appAlarmServer', 'analysisServer','CoreService','appStrainService','$timeout','CoreLoading'];
})();